%%%-------------------------------------------------------------------
%% @doc erlxlsx public API
%% @end
%%%-------------------------------------------------------------------

-module(erlxlsx).
-include_lib("kernel/include/logger.hrl").


-export([start/0, stop/0]).
-export([load_all/1, load_one/1]).

-export([test_/0]).

start() ->
    {ok, _} = application:ensure_all_started(erlxlsx),
    ok.

stop() ->
    application:stop(erlxlsx),
    ok.

%% use sys.config config, Fun(FileName, Table) by loaded all table.
load_all(Fun) ->
    Config = application:get_all_env(erlxlsx),
    Folder = proplists:get_value(folder, Config),
    Filter = proplists:get_value(filter, Config),
    MultiP = proplists:get_value(multi_process, Config),
    gen_server:cast(erlxlsx_worker, {load_all, Folder, Filter, MultiP, Fun}).

%% -spec load_one( path+file ) -> Table.
load_one(File) ->
    gen_server:call(erlxlsx_worker, {load_one, File}).



%%%-------------------------------------------------------------------
test_() ->
    start(),
    application:set_env(erlxlsx, binary_string, true),
    application:set_env(erlxlsx, sheet1_only, true),
    load_all(fun(F, T) -> ?LOG_NOTICE("file name: ~p", [F]), ?LOG_NOTICE("table: ~p", [T]) end),
    timer:sleep(1000),
    application:set_env(erlxlsx, binary_string, false),
    application:set_env(erlxlsx, sheet1_only, false),
    ?LOG_NOTICE("file name: ~p", ["./xlsx/example.xlsx"]),
    ?LOG_NOTICE("table: ~p", [erlxlsx:load_one("./xlsx/example2_config.xlsx")]),
    stop().

